import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

def pytest_addoption(parser):
    parser.addoption("--username", action="store", default="username001", help="用户名")
    parser.addoption("--email", action="store", default="1@163.com", help="邮箱")
    parser.addoption("--password", action="store", default="12345678", help="密码")
    parser.addoption("--confirm_password", action="store", default="12345678", help="确认密码")
    parser.addoption("--mobile_phone", action="store", default="18734030001", help="手机号")
    parser.addoption("--verification_code", action="store", default="1224", help="验证码")

@pytest.fixture
def test_data(request):
    return {
        "username": request.config.getoption("--username"),
        "email": request.config.getoption("--email"),
        "password": request.config.getoption("--password"),
        "confirm_password": request.config.getoption("--confirm_password"),
        "mobile_phone": request.config.getoption("--mobile_phone"),
        "verification_code": request.config.getoption("--verification_code")
    }

def test_register_01(test_data):
    # 预期 URL
    expected_url = "http://127.0.0.1:8000/login/"

    # 1. 打开浏览器
    wd = webdriver.Edge()
    wd.implicitly_wait(10)
    time.sleep(2)
    wd.get("http://127.0.0.1:8000/register/")

    try:
        # 2. 填写注册信息
        wd.find_element(By.ID, 'id_username').send_keys(test_data["username"])
        wd.find_element(By.ID, 'id_email').send_keys(test_data["email"])
        wd.find_element(By.ID, 'id_password').send_keys(test_data["password"])
        wd.find_element(By.ID, 'id_confirm_password').send_keys(test_data["confirm_password"])
        wd.find_element(By.ID, 'id_mobile_phone').send_keys(test_data["mobile_phone"])
        # 3. 点击获取验证码按钮
        wd.find_element(By.ID, 'btnSms').click()
        # 4. 填写验证码
        wd.find_element(By.ID, 'id_code').send_keys(test_data["verification_code"])
        # 5. 点击注册按钮
        wd.find_element(By.ID, 'btnSubmit').click()

        time.sleep(3)  # 等待页面加载完成

        # 6. 验证是否成功跳转到登录页面
        current_url = wd.current_url
        assert current_url == expected_url, "注册失败"

    except Exception as e:
        print(f"测试过程中发生错误: {e}")
    finally:
        wd.quit()


# class Test_Register:
#     def test_register_01(self):
#         # 测试数据
#         test_data = {
#             "username": "username001",
#             "email": "1@163.com",
#             "password": "12345678",
#             "confirm_password": "12345678",
#             "mobile_phone": "18734030001",
#             "verification_code": "1224"
#         }
#
#         # 预期 URL
#         expected_url = "http://127.0.0.1:8000/login/"
#
#         # 1. 打开浏览器
#         wd = webdriver.Edge()
#         wd.implicitly_wait(10)
#         time.sleep(2)
#         wd.get("http://127.0.0.1:8000/register/")
#
#         try:
#             # 2. 填写注册信息
#             wd.find_element(By.ID, 'id_username').send_keys(test_data["username"])
#             wd.find_element(By.ID, 'id_email').send_keys(test_data["email"])
#             wd.find_element(By.ID, 'id_password').send_keys(test_data["password"])
#             wd.find_element(By.ID, 'id_confirm_password').send_keys(test_data["confirm_password"])
#             wd.find_element(By.ID, 'id_mobile_phone').send_keys(test_data["mobile_phone"])
#             # 3. 点击获取验证码按钮
#             wd.find_element(By.ID, 'btnSms').click()
#             # 4. 填写验证码
#             wd.find_element(By.ID, 'id_code').send_keys(test_data["verification_code"])
#             # 5. 点击注册按钮
#             wd.find_element(By.ID, 'btnSubmit').click()
#
#             time.sleep(3)  # 等待页面加载完成
#
#             # 6. 验证是否成功跳转到登录页面
#             current_url = wd.current_url
#             assert current_url == expected_url, "注册失败"
#
#         except Exception as e:
#             print(f"测试过程中发生错误: {e}")
#         finally:
#             wd.quit()
#
#     def test_register_02(self):
#         # 测试数据
#         test_data = {
#             "username": "",
#             "email": "2@163.com",
#             "password": "12345678",
#             "confirm_password": "12345678",
#             "mobile_phone": "18734030002",
#             "verification_code": "1224"
#         }
#
#         # 预期 URL
#         expected_url = "http://127.0.0.1:8000/login/"
#
#         # 1. 打开浏览器
#         wd = webdriver.Edge()
#         wd.implicitly_wait(10)
#         time.sleep(2)
#         wd.get("http://127.0.0.1:8000/register/")
#
#         try:
#             # 2. 填写注册信息
#             wd.find_element(By.ID, 'id_username').send_keys(test_data["username"])
#             wd.find_element(By.ID, 'id_email').send_keys(test_data["email"])
#             wd.find_element(By.ID, 'id_password').send_keys(test_data["password"])
#             wd.find_element(By.ID, 'id_confirm_password').send_keys(test_data["confirm_password"])
#             wd.find_element(By.ID, 'id_mobile_phone').send_keys(test_data["mobile_phone"])
#             # 3. 点击获取验证码按钮
#             wd.find_element(By.ID, 'btnSms').click()
#             # 4. 填写验证码
#             wd.find_element(By.ID, 'id_code').send_keys(test_data["verification_code"])
#             # 5. 点击注册按钮
#             wd.find_element(By.ID, 'btnSubmit').click()
#
#             time.sleep(3)  # 等待页面加载完成
#
#             # 6. 验证是否成功跳转到登录页面
#             current_url = wd.current_url
#             assert current_url == expected_url, "注册失败"
#
#         except Exception as e:
#             print(f"测试过程中发生错误: {e}")
#         finally:
#             wd.quit()

